package com.industrial.premu.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.industrial.common.core.utils.StringUtils;
import com.industrial.common.core.utils.poi.ExcelUtil;
import com.industrial.common.core.utils.uuid.IdUtils;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.core.web.page.TableDataInfo;
import com.industrial.common.log.annotation.Log;
import com.industrial.common.log.enums.BusinessType;
import com.industrial.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.industrial.premu.domain.PremuPioneerCompany;
import com.industrial.premu.service.IPremuPioneerCompanyService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 亩产先锋企业Controller
 * 
 * @author ZHOUS
 * @date 2024-03-28
 */
@RestController
@RequestMapping("/pioneerCompany")
public class PremuPioneerCompanyController extends BaseController
{
    @Autowired
    private IPremuPioneerCompanyService premuPioneerCompanyService;
    // 校验企业社会信用代码
    public boolean validateCreditCode(String code) {
        String regex = "^[A-Z0-9]{18}$";
        if(StringUtils.isBlank(code)){
            return false;
        }
        return code.matches(regex);
    }
    /**
     * 查询亩产先锋企业列表
     */

    @GetMapping("/list")
    public TableDataInfo list(PremuPioneerCompany premuPioneerCompany)
    {
        startPage();
        if(StringUtils.isNull(premuPioneerCompany.getCompanyName())){
             if(validateCreditCode(premuPioneerCompany.getCompanyName())){
                 premuPioneerCompany.setCompanyName("");
                 premuPioneerCompany.setUscc(premuPioneerCompany.getCompanyName());
             }
        }
        List<PremuPioneerCompany> list = premuPioneerCompanyService.selectPremuPioneerCompanyList(premuPioneerCompany);
        return getDataTable(list);
    }


    @GetMapping("/companyTop10")
    public List<PremuPioneerCompany> companyTop10(PremuPioneerCompany premuPioneerCompany)
    {
        List<PremuPioneerCompany> list = premuPioneerCompanyService.companyTop10(premuPioneerCompany);
        return list;
    }

    /**
     * 导出亩产先锋企业列表
     */
    @RequiresPermissions("@ss.hasPermi('premu:company:export')")
    @Log(title = "亩产先锋企业", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PremuPioneerCompany premuPioneerCompany)
    {
        List<PremuPioneerCompany> list = premuPioneerCompanyService.selectPremuPioneerCompanyList(premuPioneerCompany);
        ExcelUtil<PremuPioneerCompany> util = new ExcelUtil<PremuPioneerCompany>(PremuPioneerCompany.class);
        util.exportExcel(response, list, "亩产先锋企业数据");
    }

    /**
     * 获取亩产先锋企业详细信息
     */

    @GetMapping(value = "/{baseId}")
    public AjaxResult getInfo(@PathVariable("baseId") String baseId)
    {
        return success(premuPioneerCompanyService.selectPremuPioneerCompanyByBaseId(baseId));
    }

    /**
     * 新增亩产先锋企业
     */

    @Log(title = "亩产先锋企业", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PremuPioneerCompany premuPioneerCompany)
    {
        return toAjax(premuPioneerCompanyService.insertPremuPioneerCompany(premuPioneerCompany));
    }

    /**
     * 修改亩产先锋企业
     */

    @Log(title = "亩产先锋企业", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PremuPioneerCompany premuPioneerCompany)
    {
        return toAjax(premuPioneerCompanyService.updatePremuPioneerCompany(premuPioneerCompany));
    }

    /**
     * 删除亩产先锋企业
     */

    @Log(title = "亩产先锋企业", businessType = BusinessType.DELETE)
	@DeleteMapping("/{baseIds}")
    public AjaxResult remove(@PathVariable String[] baseIds)
    {
        return toAjax(premuPioneerCompanyService.deletePremuPioneerCompanyByBaseIds(baseIds));
    }
    // 下载模板
    @GetMapping("/importTemplate/mcxfqy")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<PremuPioneerCompany> util = new ExcelUtil<PremuPioneerCompany>(PremuPioneerCompany.class);
        util.importTemplateExcel(response, "亩产先锋企业","亩产先锋企业");
    }

    /**
     * 导入
     */
    @Log(title = "导入亩产先锋企业", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
            ExcelUtil<PremuPioneerCompany> util = new ExcelUtil<PremuPioneerCompany>(PremuPioneerCompany.class);
            List<PremuPioneerCompany> list = util.importExcel(file.getInputStream(),1);
            int flag=0;
            for (int i = 0; i < list.size(); i++) {
                String id= IdUtils.simpleUUID();
                list.get(i).setBaseId(id);
                flag+=  premuPioneerCompanyService.insertPremuPioneerCompany(list.get(i));
            }
        return AjaxResult.success("成功导入"+flag+"条数据！");
    }

    @GetMapping("/queryTop15Industry")
    public AjaxResult queryTop15Industry(){
        List<Map<String, BigDecimal>> maps = premuPioneerCompanyService.queryTop15Industry();
        return AjaxResult.success(maps);
    }
}
